package com.gzyj.flink.dto.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy-MM-dd");

    public static Date MIN_VALUE() {

        return new Date(0);

    }

    public static Date toDate(String str) {
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toStr(Date str) {

        return sdf.format(str);

    }

    public static String getTotay() {
        return datesdf.format(new Date());
    }

    public static Date getToday() {
        try {
            return datesdf.parse(getTotay());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String parseDay(Date date) {

        return datesdf.format(date);

    }

    private final static SimpleDateFormat HOUR = new SimpleDateFormat("HH:mm:ss");

    /// <summary>
    /// 判断时间是否在某个时间范围内
    /// </summary>
    public static boolean InTimeRange(String gpsTime, String sTime, String eTime) {
        try {
            Date tsGpsTime = HOUR.parse(gpsTime);
            Date tsBeginTime = HOUR.parse(sTime);
            Date tsEndTime = HOUR.parse(eTime);
            //跨零点
            if (tsBeginTime.after(tsEndTime)) {
                if (tsGpsTime.after(tsBeginTime) && tsGpsTime.before(HOUR.parse("23:59:59")))
                    return true;
                else if (tsGpsTime.after(HOUR.parse("00:00:00")) && tsGpsTime.before(tsEndTime))
                    return true;
                else
                    return false;
            } else {
                return (tsGpsTime.after(tsBeginTime) && tsGpsTime.before(tsEndTime)) ? true : false;
            }
        } catch (Exception ex) {
            return false;
        }
    }
}

